package com.gxa.order.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gxa.order.dao.OrderFightDao;
import com.gxa.order.param.MacOrderParam;
import com.gxa.order.param.MemberParam;
import com.gxa.order.service.CheckService;
import com.gxa.order.service.TicketService;
import com.gxa.order.service.UserService;
import dto.ResultDTO;
import entity.Flight;
import entity.MacOrder;
import com.gxa.order.dao.MacOrderDao;
import com.gxa.order.service.MacOrderService;
import entity.Member;
import entity.OrderFight;
import exception.SystemException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import util.Response;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * (MacOrder)表服务实现类
 *
 * @author makejava
 * @since 2021-05-11 09:22:44
 */
@Service("macOrderService")
@SuppressWarnings("all")
public class MacOrderServiceImpl implements MacOrderService {
    @Resource
    private MacOrderDao macOrderDao;
    @Resource
    private UserService userService;
    @Resource
    private TicketService ticketService;
    @Resource
    private OrderFightDao orderFightDao;
    @Resource
    private CheckService checkService;
//    @Autowired
//    private RocketMQTemplate rocketMQTemplate;

    /**
     * 通过ID查询单条数据
     *
     * @param macOrderId 主键
     * @return 实例对象
     */
    @Override
    public ResultDTO queryById(Integer macOrderId) {
        if (macOrderId == null) {
            throw new SystemException(1001, "没有订单id");
        }
        MacOrder macOrder = this.macOrderDao.queryById(macOrderId);
        return Response.success(200, "成功！", macOrder);

    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<MacOrder> queryAllByLimit(int offset, int limit) {
        return this.macOrderDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param macOrder 实例对象
     * @return 实例对象
     */
    @Override
    public MacOrder insert(MacOrder macOrder) {
        this.macOrderDao.insert(macOrder);
        return macOrder;
    }

    /**
     * 修改数据
     *
     * @param macOrder 实例对象
     * @return 实例对象
     */
    @Override
    public ResultDTO update(MacOrder macOrder) {
        this.macOrderDao.update(macOrder);
        return this.queryById(macOrder.getMacOrderId());
    }

    /**
     * 通过主键删除数据
     *
     * @param macOrderId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer macOrderId) {
        return this.macOrderDao.deleteById(macOrderId) > 0;
    }

    @Override
    public ResultDTO addMacOrder(MacOrderParam param) {
        if (param == null) {
            throw new SystemException(1001, "传的值不能为空!");
        }
        if (param.getApplyId() == null && param.getMember().getMemberId() == null && param.getFightIds() == null) {
            throw new SystemException(1001, "传参不正确！");
        }
        //查询申请人是否存在，根据申请人id
        ResultDTO resApply = userService.findAllById(param.getApplyId());
        System.out.println(resApply);
        if (resApply.getData()==null) {
            throw new SystemException(1001,"声请人不存在");
        }
        Member member1 = param.getMember();
        System.out.println(member1);
        System.out.println("____________________________________________");

        //根据乘机人信息，查询是否存在
        ResultDTO resMember = userService.queryMember(member1);
        System.out.println(resMember);
        System.out.println("____________________________________________");
        if (resMember.getData()==null) {
            throw new SystemException(1002,"乘机人不存在");
        }
        //获取返回的乘机人对象
        ObjectMapper objectMapper = new ObjectMapper();
        Member member = objectMapper.convertValue(resMember.getData(), Member.class);
//        Integer memberId2 = Integer.valueOf(memberId3);
        System.out.println(member);
        System.out.println("____________________________________________");
        //生成订单编号
        String orderCode = getOrderCode(param.getApplyId());
        //生成订单对象，赋值
        MacOrder macOrder = new MacOrder();
        macOrder.setMacOrderCode(orderCode);
        macOrder.setMemberId(member.getMemberId());
//        macOrder.setMemberId(param.getApplyId());
        macOrder.setApplyId(param.getApplyId());
        macOrder.setCreateTime(new Timestamp(System.currentTimeMillis()));
        int i = macOrderDao.addOrder(macOrder);
        if (i<=0){
            throw new SystemException(1003,"添加订单失败");
        }
        if (param.getFightIds() == null){
            throw new SystemException(1001, "你还没有请求机票！");
        }
        for (Integer fightId : param.getFightIds()) {
            ResultDTO resFight = ticketService.findByFlightId(fightId);
            System.out.println(resFight);
            if (resFight.getData()==null) {
                throw new SystemException(1004,"机票不存在");
            }
            Flight flight = objectMapper.convertValue(resFight.getData(), Flight.class);
            if (flight.getFightCount()<=0) {
                throw new SystemException(1005,"机票存量不足");
            }
            ResultDTO flightDTO = ticketService.selectCount(fightId);
            if (flightDTO.getData()==null) {
                throw new SystemException(1006,"机票存量不足");
            }
            OrderFight orderFight = new OrderFight();
            orderFight.setMacOrderId(macOrder.getMacOrderId());
            orderFight.setFightId(fightId);
            orderFight.setFightPrice(flight.getFlightMoney());
            int i1 = orderFightDao.addOrderFight(orderFight);
            if (i1<=0){
                throw new SystemException(1007,"创建订单失败");
            }
        }
        System.out.println("------------------"+macOrder.getMacOrderId());

        ResultDTO resCheck = checkService.addMac(macOrder.getMacOrderId());
        System.out.println(resCheck);
        if (resCheck.getData()==null){
            throw new SystemException(1008,"创建审批单失败");
        }

//        Message message = new GenericMessage(macOrder.getMacOrderId());
//
//        rocketMQTemplate.syncSend("order-topic",message,6000,5);

        return Response.success("创建订单成功");
    }

    @Override
    public ResultDTO findUserAndFlight(Integer memberId, Integer[] flightIds) {

        MemberParam memberParam = new MemberParam();
        System.out.println(memberId);
        ArrayList<Flight> flightList = new ArrayList<>();
        //查询申请人是否存在，根据申请人id
        ResultDTO resApply = userService.findAllById(memberId);
        if (resApply.getData()==null) {
            throw new SystemException(1001,"声请人不存在");
        }
        //获取返回的对象
        ObjectMapper objectMapper = new ObjectMapper();
        Member member = objectMapper.convertValue(resApply.getData(), Member.class);
        memberParam.setMember(member);
        for (Integer flightId : flightIds) {
            if (flightId != null) {
                ResultDTO resFight = ticketService.findByFlightId(flightId);
                System.out.println(resFight);
                if (resFight.getData()==null) {
                    throw new SystemException(1004,"机票不存在");
                }
                Flight flight = objectMapper.convertValue(resFight.getData(), Flight.class);
                flightList.add(flight);
                memberParam.setAllMoney(memberParam.getAllMoney()+flight.getFlightMoney());
            }
        }
        memberParam.setFlightList(flightList);
        return Response.success(200,"查询成功",memberParam);
    }

    public String getOrderCode(Integer applyId){
        return "编号"+applyId+System.currentTimeMillis();
    }



}
